const host: string = '/api'

const two = (obj: any): string => {
  let arr = []
  for (let i in obj) {
    arr.push(i + '=' + obj[i])
  }
  return arr.join('&')
}

const http = async (json: {
  url: string,
  method: string,
  params?: any,
  data?: any
}) => {
  let token = '' + sessionStorage.getItem('token')
  let option:{method:string,headers:{token:string,'Content-Type':string},body?:any} = {
      method: json.method, 
      headers: {
        'Content-Type': 'application/json',
        token:token
      }
  }
  if(option.method === 'post') option.body = JSON.stringify(json.data)
  const response = await fetch(host + json.url + '?' + two(json.data),option)
  return await response.json()
}
export default http